using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Decav.Gatsb.Mobile;
using Decav.Gatsb.Configuration;
using System.IO;
using Decav.Gatsb.Application.Configuration;
using System.Text;
using Decav.Gatsb.Data;

namespace Decav.Gatsb.Application.CommandWorkflow
{
	public sealed partial class ExecuteCommandWorkflow: SequentialWorkflowActivity
	{
		public ExecuteCommandWorkflow()
		{
			InitializeComponent();

            if (GatsbConfigurationSection.Current != null)
                DefaultMailAddress = GatsbConfigurationSection.Current.NotificationFromAddress;
		}

        /// <summary>
        /// The dependency property that gets or sets the message that the workflow executes against.
        /// </summary>
        public static DependencyProperty InputMessageProperty = DependencyProperty.Register(
            "InputMessage", typeof(MobileMessage), typeof(ExecuteCommandWorkflow));

        /// <summary>
        /// Gets or sets the message that the workflow executes against.
        /// </summary>
        public MobileMessage InputMessage
        {
            get { return (MobileMessage)GetValue(InputMessageProperty); }
            set { SetValue(InputMessageProperty, value); }
        }

        /// <summary>
        /// Gets the help text.
        /// </summary>
        public string HelpText
        {
            get
            {
                if (DesignMode)
                    return "help text";

                return File.ReadAllText(GatsbApplicationConfigurationSection.Current.HelpTextPath);
            }
        }

        /// <summary>
        /// The dependency property that gets or sets the default mail address.
        /// </summary>
        public static DependencyProperty DefaultMailAddressProperty = DependencyProperty.Register(
            "DefaultMailAddress", typeof(string), typeof(ExecuteCommandWorkflow));

        /// <summary>
        /// Gets or sets the default mail address.
        /// </summary>
        public string DefaultMailAddress
        {
            get { return (string)GetValue(DefaultMailAddressProperty); }
            set { SetValue(DefaultMailAddressProperty, value); }
        }

        private void createLocationInfoMessage_ExecuteCode(object sender, EventArgs e)
        {
            Location result = findLocation.Location;
            Post[] recentPosts = null;
            User[] visitors = null;

            using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
            {
                recentPosts = adapt.GetLatestLocationPosts(result.Id, 10);
                visitors = adapt.GetLocationCurrentVisitors(result.Id);
            }

            StringBuilder builder = new StringBuilder();
            builder.AppendLine(result.Name);
            builder.AppendLine(result.Address.Address1);
            builder.AppendLine(result.TelephoneNumber.ToString());
            builder.AppendLine();

            if (recentPosts.Length > 0)
                builder.AppendFormat("{0} recent posts\r\n", recentPosts.Length);
            else
                builder.AppendLine("No recent posts");

            if (visitors.Length > 0)
                builder.AppendFormat("{0} visitors\r\n", visitors.Length);
            else
                builder.AppendLine("No visitors");

            sendInfo.MessageBody = builder.ToString();
        }

        private void removeFirstMsgLine_ExecuteCode(object sender, EventArgs e)
        {
            // Cleans out the first line of the message that was in use by another command (such as set location)
            string newBody = InputMessage.Body.Trim();
            newBody = newBody.Substring(MessageParser.GetFirstLine(InputMessage).Length);
            InputMessage.Body = newBody.Trim();
        }

        private void prepareAuthFaultMessage_ExecuteCode(object sender, EventArgs e)
        {
            string authFaultMessage = "We could not figure out who you are from your message.\r\n\r\nIf you have not signed up yet, text 'hello' to Gatsb!";
            sendAuthFaultMessage.MessageBody = authFaultMessage;
            sendAuthFaultMessage.RecipientAddress = InputMessage.SendingAddress.ToString();
            sendAuthFaultMessage.SendingAddress = GatsbConfigurationSection.Current.NotificationFromAddress;
        }
        
	}

}
